﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Extensions;
using FRL.DebtChased.Repository;
using FRL.DebtChased.Silverlight.Library;
using FRL.DebtChased.WorkflowObjects;

namespace FRL.DebtChased.WorkflowEditor.WorkflowItemEditors
{

	/// <summary>
	/// Declaration of SendLetterFaxCtrl class
	/// Controls for editing the workflow action to send a letter or fax to the debtor, client, agent or solicitor
	/// </summary>
	public partial class SendLetterFaxCtrl : ActionBaseCtrl
	{

		/////////////////////////////////////////////////////////////
		// Construction

		public SendLetterFaxCtrl()
		{

			// Initialize resources
			InitializeComponent();

		}

		/////////////////////////////////////////////////////////////
		// Implementation

		public override List<string> Validate()
		{

			// Declare return variable
			List<string> Ftmp = null;

			// Must have a comm-body
			if (radCreateFromTemplate.IsChecked ?? false)
			{
				if(ctrlCommBody.IsEmpty)
					Extensions.Extensions.SafeAddString(ref Ftmp,"Select a letter body.");
			}
			if (chkByEmail.IsChecked ?? false)
			{
				if(ctrlCoverCommBody.IsEmpty)
					Extensions.Extensions.SafeAddString(ref Ftmp, "Select a cover body for the email.");
			}
			if (radSendPreviousLetter.IsChecked ?? false)
			{
				if ((cboResendLetterType.SelectedValue as ComboBoxObject).ItemData == 0)
					Extensions.Extensions.SafeAddString(ref Ftmp, "Enter the type of the document to resend.");
			}

			// Return variable
			return Ftmp;

		}

		public override void UpdateControls(WorkflowAction_Base action)
		{

			// Block update events
			this.BlockUpdateEvents = true;
			{

				// Cast the action
				WorkflowAction_SendLetterFax sendLetterFaxAction = action as WorkflowAction_SendLetterFax;
#if DEBUG
				Debug.Assert(sendLetterFaxAction != null);
#endif // #if DEBUG

				// Set controls - target
				ComboBoxObject.SelectByItemData(cboTarget, (int)sendLetterFaxAction.Target);

				// Set controls - content type
				radCreateFromTemplate.IsChecked = (sendLetterFaxAction.Content == WorkflowAction_SendLetterFax.eContent.Template);
				radSendPreviousLetter.IsChecked = (sendLetterFaxAction.Content == WorkflowAction_SendLetterFax.eContent.Resend);

				// Set controls - comm body
				ctrlCommBody.Set(sendLetterFaxAction.CommBody);

				// Set controls - new letter tag
				ComboBoxObject.SelectByItemData(cboNewLetterType, sendLetterFaxAction.NewLetterTypeId);

				// Set controls - resend letter tags
				ComboBoxObject.SelectByItemData(cboResendLetterType, sendLetterFaxAction.ResendLetterTypeId);

				// Set transport check boxes
				chkByPost.IsChecked = ((sendLetterFaxAction.TravelBy & WorkflowAction_SendLetterFax.eTravelBy.Post) != 0);
				chkByFax.IsChecked = ((sendLetterFaxAction.TravelBy & WorkflowAction_SendLetterFax.eTravelBy.Fax) != 0);
				chkByEmail.IsChecked = ((sendLetterFaxAction.TravelBy & WorkflowAction_SendLetterFax.eTravelBy.Email) != 0);

				// Set controls - cover comm body
				ctrlCoverCommBody.Set(sendLetterFaxAction.CoverCommBody);

			}
			this.BlockUpdateEvents = false;

			// Setup controls
			_setupControls();

		}

		public override void UpdateFromControls(ref WorkflowAction_Base action)
		{

			// If the action needs changing...
			if (!(action is WorkflowAction_SendLetterFax))
				action = new WorkflowAction_SendLetterFax();

			// Cast the action
			WorkflowAction_SendLetterFax sendLetterFaxAction = action as WorkflowAction_SendLetterFax;
#if DEBUG
			Debug.Assert(sendLetterFaxAction != null);
#endif // #if DEBUG

			// target
			sendLetterFaxAction.Target = (Definitions.eCommTargetType)(cboTarget.SelectedItem as ComboBoxObject).ItemData;

			// Content type
			sendLetterFaxAction.Content = (radCreateFromTemplate.IsChecked ?? false) ? WorkflowAction_SendLetterFax.eContent.Template : WorkflowAction_SendLetterFax.eContent.Resend;

			// Set controls - comm body
			ctrlCommBody.Get(sendLetterFaxAction.CommBody);

			// Set controls - new letter tag
			sendLetterFaxAction.NewLetterTypeId = (cboNewLetterType.SelectedItem as ComboBoxObject).ItemData;

			// Set controls - resend letter tags
			sendLetterFaxAction.ResendLetterTypeId = (cboResendLetterType.SelectedItem as ComboBoxObject).ItemData;

			// Set transport check boxes
			sendLetterFaxAction.TravelBy = WorkflowAction_SendLetterFax.eTravelBy.NotSet;
			if (chkByPost.IsChecked ?? false)
				sendLetterFaxAction.TravelBy |= WorkflowAction_SendLetterFax.eTravelBy.Post;
			if (chkByFax.IsChecked ?? false)
				sendLetterFaxAction.TravelBy |= WorkflowAction_SendLetterFax.eTravelBy.Fax;
			if (chkByEmail.IsChecked ?? false)
				sendLetterFaxAction.TravelBy |= WorkflowAction_SendLetterFax.eTravelBy.Email;

			// Set controls - cover comm body
			ctrlCoverCommBody.Get(sendLetterFaxAction.CoverCommBody);

		}

		public override void InitializeControls()
		{

			// Set controls - comm body controls
			ctrlCommBody.InitializeControls(CachedData.Singleton.CommBodies);
			ctrlCoverCommBody.InitializeControls(CachedData.Singleton.CommBodies);

			// Set controls - New Letter Type
			ComboBoxObject.LoadFromDictionary(cboNewLetterType,CachedData.Singleton.LetterTypeDictionary);
			cboNewLetterType.SelectedIndex = 0;

			// Set controls - Resend Letter Tags
			ComboBoxObject.LoadFromDictionary(cboResendLetterType, CachedData.Singleton.LetterTypeDictionary);
			cboResendLetterType.SelectedIndex = 0;

			// Set controls - target
			TransportSupport.LoadCommTargetTypeControl(cboTarget, Definitions.eCommTargetType.Debtor);

			// Set controls - content type
			radCreateFromTemplate.IsChecked = true;
			radSendPreviousLetter.IsChecked = false;

			// Set transport check boxes
			chkByPost.IsChecked = true;
			chkByFax.IsChecked = false;
			chkByEmail.IsChecked = false;

			// unblock update events
#if DEBUG
			Debug.Assert(this.BlockUpdateEvents);
#endif // #if DEBUG
			this.BlockUpdateEvents = false;

			// setup controls
			_setupControls();

		}

		/////////////////////////////////////////////////////////////
		// Event handlers

		private new void ActionBaseCtrl_Loaded(object sender, RoutedEventArgs e)
		{

			// Call base-class function
			base.ActionBaseCtrl_Loaded(sender,e);

			// setup controls
			_setupControls();

		}

		private void ContentRadio_Click(object sender, RoutedEventArgs e) { _controlChange(); }
		private void cboTarget_SelectionChanged(object sender, SelectionChangedEventArgs e) { _controlChange(); }
		private void CommBodyCtrl_SelectionChanged(object sender, EventArgs e) { _controlChange(); }
		private void TransportMethod_Click(object sender, RoutedEventArgs e) { _controlChange(); }
		private void cboNewLetterType_SelectionChanged(object sender, SelectionChangedEventArgs e) { _controlChange(); }
		private void cboResendLetterType_SelectionChanged(object sender, SelectionChangedEventArgs e) { _controlChange(); }

		/////////////////////////////////////////////////////////////
		// Operations

		private void _controlChange()
		{

			// Shortcuts
			if (this.BlockUpdateEvents)
				return;

			// setup controls
			_setupControls();

			// If this control is in a tabbed panel...
			if (m_pnlAddAction != null)
				m_pnlAddAction.UpdateFromCtrl(this);

		}

		private void _setupControls()
		{

			// Show/hide controls
			pnlNewLetter.Visibility = (radCreateFromTemplate.IsChecked ?? false) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
			pnlResend.Visibility = (radSendPreviousLetter.IsChecked ?? false) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;

			// Show/hide controls- Email Options
			ctrlCoverCommBody.Visibility = ((chkByEmail.IsChecked ?? false)) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
			lbEmailCoverTemplate.Visibility = ctrlCoverCommBody.Visibility;
				
		}

		private void ctrlCoverCommBody_SelectionChanged(object sender, EventArgs e)
		{

		}

	}

}
